﻿namespace Smart.LiveUpdate
{
    using Smart.LiveUpdate.DS;
    using Smart.Utils.Net;
    using Smart.Utils.Utils;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Threading;
    using System.Windows.Forms;

    public class xfmMyUpdate : Form
    {
        private ProgressBar Bar;
        private Button btnCancel;
        private Button btnCheck;
        private Button btnHelp;
        private IContainer components = null;
        private string fileinfo = (Application.StartupPath + @"\updateinfo.xml");
        private string FileName = "";
        private Label lblMessage;
        private Label lblNewversion;
        private Label lblNewVersionLbl;
        private Label lblRemain;
        private Label lblSize;
        private Label lblSpeed;
        private ToolStripStatusLabel lblStatus;
        private Label lblTitle;
        private Label lblVersion;
        private Label lblVersionLbl;
        private ArrayList List = new ArrayList();
        private string m_path = Application.StartupPath;
        private NotifyIcon notify;
        private string Soft = "";
        private StatusStrip statusStrip1;
        private string URL = "";

        public xfmMyUpdate()
        {
            this.InitializeComponent();
            dsUPDATE supdate = new dsUPDATE();
            if (File.Exists(this.fileinfo))
            {
                supdate.ReadXml(this.fileinfo);
                if (supdate.UPDATE.Rows.Count > 0)
                {
                    this.lblTitle.Text = supdate.UPDATE.Rows[0]["ApplicationName"].ToString();
                    this.lblVersion.Text = supdate.UPDATE.Rows[0]["OldVersion"].ToString();
                    this.lblNewversion.Text = "?.?.?.?";
                    this.URL = supdate.UPDATE.Rows[0]["UpdateLink"].ToString();
                    this.m_path = supdate.UPDATE.Rows[0]["ApplicationPath"].ToString();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.btnCancel.Text == "Kết Th\x00fac")
            {
                Application.ExitThread();
                Environment.Exit(0);
            }
            DownLoadHelper.Cancle = true;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (this.btnCheck.Text == "Cập Nhật")
            {
                this.btnCancel.Text = "Hủy Bỏ";
                this.lblMessage.Text = "Đang Tiến H\x00e0nh Cập Nhật!";
                this.Bar.Visible = true;
                this.btnCheck.Enabled = false;
                this.Download(this.List[0].ToString());
            }
            else
            {
                this.lblMessage.Text = "Đang Kiểm Tra Kết Nối Internet...";
                if (!Internet.IsConnectedToInternet())
                {
                    this.lblMessage.Text = "M\x00e1y T\x00ednh Kh\x00f4ng Kết Nối Internet!";
                }
                this.lblMessage.Text = "Đang Kết Nối Tới M\x00e1y Chủ...";
                DownLoadHelper.Cancle = false;
                DownLoadHelper helper = new DownLoadHelper(this.URL);
                helper.CompleteDownload += new DownLoadHelper.CompleteDownloadEventHander(this.Dd_CompleteDownload);
                helper.DownloadProcess += new DownLoadHelper.DownloadProcessEventHander(this.Dd_DownloadProcess);
                helper.CancleDownload += new DownLoadHelper.CancleDownloadEventHander(this.Dd_CancleDownload);
                this.lblMessage.Text = "Đang Kiểm Tra Phi\x00ean Bản...";
                helper.Start();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.btnCancel.Text = "Hủy Bỏ";
            this.Download(this.List[0].ToString());
        }

        private void Dd_CancleDownload(object sender, string e)
        {
            this.lblMessage.Text = "Kết nối tới Smart.com.vn đ\x00e3 bị ngắt.";
        }

        private void Dd_CompleteDownload(object sender)
        {
            FileInfo info = new FileInfo(Application.StartupPath + @"\Version.txt");
            if (info.Exists)
            {
                StreamReader reader = new StreamReader(Application.StartupPath + @"\Version.txt");
                try
                {
                    string str;
                    if (((str = reader.ReadLine()) != "0") && str.StartsWith("Version:"))
                    {
                        this.lblNewversion.Text = str.Replace("Version:", "").Trim();
                        if (string.Compare(this.lblNewversion.Text, this.lblVersion.Text, true) > 0)
                        {
                            this.btnCheck.Enabled = true;
                            str = reader.ReadLine();
                            if (str.StartsWith("http://"))
                            {
                                this.lblMessage.Text = "Ho\x00e0n Tất!";
                                this.Bar.Visible = true;
                                this.lblMessage.Text = "Sẵn S\x00e0ng Tiến H\x00e0nh Cập Nhật.";
                                this.btnCheck.Text = "Cập Nhật";
                                this.List.Add(str);
                            }
                        }
                        else
                        {
                            this.Dd_CancleDownload(this, "Phi\x00ean Bản Hiện Tại Đ\x00e3 Mới Nhất.");
                            this.btnCheck.Enabled = false;
                        }
                    }
                }
                catch
                {
                }
                reader.Close();
            }
        }

        private void Dd_DownloadProcess(object sender, long length, long position, double percent, double speed)
        {
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void Download(string URL)
        {
            this.Bar.Value = 0;
            this.btnCheck.Enabled = false;
            this.btnCheck.Enabled = false;
            DownLoadHelper.Cancle = false;
            DownLoadHelper helper = new DownLoadHelper(URL);
            this.FileName = helper.FileName;
            this.lblMessage.Text = "Đang tải tập tin " + helper.FileName + " từ Website " + helper.Website;
            helper.CompleteDownload += new DownLoadHelper.CompleteDownloadEventHander(this.Download_CompleteDownload);
            helper.DownloadProcess += new DownLoadHelper.DownloadProcessEventHander(this.Download_DownloadProcess);
            helper.CancleDownload += new DownLoadHelper.CancleDownloadEventHander(this.Download_CancleDownload);
            new Thread(new ThreadStart(helper.Start)).Start();
        }

        private void Download_CancleDownload(object sender, string e)
        {
            if (this.Bar.InvokeRequired)
            {
                DownLoadHelper.CancleDownloadEventHander method = new DownLoadHelper.CancleDownloadEventHander(this.Download_CancleDownload);
                base.Invoke(method, new object[] { sender, e });
            }
            else
            {
                this.btnCancel.Text = "Kết Th\x00fac";
                this.btnCheck.Enabled = true;
                this.btnCheck.Enabled = true;
                this.lblMessage.Text = e;
                this.lblSize.Visible = false;
                this.lblSpeed.Visible = false;
                this.lblRemain.Visible = false;
            }
        }

        private void Download_CompleteDownload(object sender)
        {
            if (this.Bar.InvokeRequired)
            {
                DownLoadHelper.CompleteDownloadEventHander method = new DownLoadHelper.CompleteDownloadEventHander(this.Download_CompleteDownload);
                base.Invoke(method, new object[] { sender });
            }
            else
            {
                this.lblMessage.Text = "Ho\x00e0n Tất";
                this.btnCheck.Enabled = true;
                this.btnCheck.Text = "Cập Nhật";
                this.Bar.Value = 0;
                this.lblSize.Visible = false;
                this.lblSpeed.Visible = false;
                this.lblRemain.Visible = false;
                string desc = Application.StartupPath + @"\Update";
                if (!Directory.Exists(desc))
                {
                    Directory.CreateDirectory(desc);
                }
                ZipHelper.UnzipProcess += new ZipHelper.UnzipProcessEventHander(this.MyUtil_UnzipProcess);
                ZipHelper.UnzipComplete += new ZipHelper.UnzipCompleteEventHander(this.MyUtil_UnzipComplete);
                ZipHelper.UnzipFail += new ZipHelper.UnzipFailEventHander(this.MyUtil_UnzipFail);
                new Thread(delegate() {
                    ZipHelper.Unzipfile(Application.StartupPath + @"\" + this.FileName, desc);
                }).Start();
            }
        }

        private void Download_DownloadProcess(object sender, long length, long position, double percent, double speed)
        {
            if (this.Bar.InvokeRequired)
            {
                DownLoadHelper.DownloadProcessEventHander method = new DownLoadHelper.DownloadProcessEventHander(this.Download_DownloadProcess);
                base.Invoke(method, new object[] { sender, length, position, percent, speed });
            }
            else
            {
                this.Bar.Value = (int) percent;
                double num = speed / 1024.0;
                this.lblSize.Text = "Size:" + string.Format("{0:##,##0.##}", (Convert.ToDouble(length) / 1024.0) / 1024.0) + " MB";
                this.lblSpeed.Text = "Speed:" + string.Format("{0:##,##0.##}", (num < 1024.0) ? num : (num / 1024.0)) + ((num < 1024.0) ? " KB" : "MB");
                this.lblRemain.Text = "Remain:" + string.Format("{0:##,##0.##}", (Convert.ToDouble((long) (length - position)) / 1024.0) / 1024.0) + " MB";
                this.lblSize.Visible = true;
                this.lblSpeed.Visible = true;
                this.lblRemain.Visible = true;
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xfmMyUpdate));
            this.lblMessage = new Label();
            this.Bar = new ProgressBar();
            this.lblVersionLbl = new Label();
            this.lblNewVersionLbl = new Label();
            this.lblNewversion = new Label();
            this.lblSize = new Label();
            this.lblSpeed = new Label();
            this.lblRemain = new Label();
            this.lblVersion = new Label();
            this.lblTitle = new Label();
            this.btnHelp = new Button();
            this.btnCancel = new Button();
            this.btnCheck = new Button();
            this.notify = new NotifyIcon(this.components);
            this.statusStrip1 = new StatusStrip();
            this.lblStatus = new ToolStripStatusLabel();
            this.statusStrip1.SuspendLayout();
            base.SuspendLayout();
            this.lblMessage.AutoSize = true;
            this.lblMessage.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblMessage.Location = new Point(12, 0x5b);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new Size(0x41, 13);
            this.lblMessage.TabIndex = 2;
            this.lblMessage.Text = "Tiến Tr\x00ecnh";
            this.Bar.Location = new Point(12, 0x70);
            this.Bar.Name = "Bar";
            this.Bar.Size = new Size(330, 20);
            this.Bar.TabIndex = 3;
            this.lblVersionLbl.AutoSize = true;
            this.lblVersionLbl.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblVersionLbl.Location = new Point(12, 0x2e);
            this.lblVersionLbl.Name = "lblVersionLbl";
            this.lblVersionLbl.Size = new Size(0x41, 13);
            this.lblVersionLbl.TabIndex = 0x2b;
            this.lblVersionLbl.Text = "Phi\x00ean Bản";
            this.lblNewVersionLbl.AutoSize = true;
            this.lblNewVersionLbl.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblNewVersionLbl.Location = new Point(12, 0x42);
            this.lblNewVersionLbl.Name = "lblNewVersionLbl";
            this.lblNewVersionLbl.Size = new Size(0x59, 13);
            this.lblNewVersionLbl.TabIndex = 0x2c;
            this.lblNewVersionLbl.Text = "Phi\x00ean Bản Mới";
            this.lblNewversion.AutoSize = true;
            this.lblNewversion.Location = new Point(0x69, 0x42);
            this.lblNewversion.Name = "lblNewversion";
            this.lblNewversion.Size = new Size(40, 13);
            this.lblNewversion.TabIndex = 0x2d;
            this.lblNewversion.Text = "3.0.0.1";
            this.lblSize.AutoSize = true;
            this.lblSize.Location = new Point(0x100, 140);
            this.lblSize.Name = "lblSize";
            this.lblSize.Size = new Size(30, 13);
            this.lblSize.TabIndex = 0x2e;
            this.lblSize.Text = "Size:";
            this.lblSize.Visible = false;
            this.lblSpeed.AutoSize = true;
            this.lblSpeed.Location = new Point(15, 140);
            this.lblSpeed.Name = "lblSpeed";
            this.lblSpeed.Size = new Size(30, 13);
            this.lblSpeed.TabIndex = 0x2f;
            this.lblSpeed.Text = "Size:";
            this.lblSpeed.Visible = false;
            this.lblRemain.AutoSize = true;
            this.lblRemain.Location = new Point(0x86, 140);
            this.lblRemain.Name = "lblRemain";
            this.lblRemain.Size = new Size(30, 13);
            this.lblRemain.TabIndex = 0x30;
            this.lblRemain.Text = "Size:";
            this.lblRemain.Visible = false;
            this.lblVersion.AutoSize = true;
            this.lblVersion.Location = new Point(0x69, 0x2e);
            this.lblVersion.Name = "lblVersion";
            this.lblVersion.Size = new Size(40, 13);
            this.lblVersion.TabIndex = 0x29;
            this.lblVersion.Text = "3.0.0.0";
            this.lblTitle.AutoSize = true;
            this.lblTitle.Font = new Font("Microsoft Sans Serif", 11.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblTitle.Location = new Point(12, 9);
            this.lblTitle.Name = "lblTitle";
            this.lblTitle.Size = new Size(180, 0x16);
            this.lblTitle.TabIndex = 40;
            this.lblTitle.Text = "Phần Mềm Quản L\x00fd Kho";
            this.lblTitle.UseCompatibleTextRendering = true;
            this.btnHelp.Image = (Image) manager.GetObject("btnHelp.Image");
            this.btnHelp.ImageAlign = ContentAlignment.MiddleLeft;
            this.btnHelp.Location = new Point(0xfe, 0xa4);
            this.btnHelp.Name = "btnHelp";
            this.btnHelp.Size = new Size(0x57, 0x1a);
            this.btnHelp.TabIndex = 0x33;
            this.btnHelp.Text = "Hướng Dẫn";
            this.btnHelp.TextAlign = ContentAlignment.MiddleRight;
            this.btnCancel.Image = (Image) manager.GetObject("btnCancel.Image");
            this.btnCancel.ImageAlign = ContentAlignment.MiddleLeft;
            this.btnCancel.Location = new Point(0x8a, 0xa4);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x57, 0x1a);
            this.btnCancel.TabIndex = 50;
            this.btnCancel.Text = "Kết Th\x00fac";
            this.btnCancel.TextAlign = ContentAlignment.MiddleRight;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnCheck.Image = (Image) manager.GetObject("btnCheck.Image");
            this.btnCheck.ImageAlign = ContentAlignment.MiddleLeft;
            this.btnCheck.Location = new Point(15, 0xa4);
            this.btnCheck.Name = "btnCheck";
            this.btnCheck.Size = new Size(0x57, 0x1a);
            this.btnCheck.TabIndex = 0x31;
            this.btnCheck.Text = "Kiểm Tra";
            this.btnCheck.TextAlign = ContentAlignment.MiddleRight;
            this.btnCheck.Click += new EventHandler(this.btnCheck_Click);
            this.notify.Icon = (Icon) manager.GetObject("notify.Icon");
            this.notify.Text = "Cập Nhật Trực Tuyến";
            this.notify.Visible = true;
            this.notify.Click += new EventHandler(this.notify_Click);
            this.statusStrip1.Items.AddRange(new ToolStripItem[] { this.lblStatus });
            this.statusStrip1.Location = new Point(0, 0xd0);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new Size(0x15f, 0x16);
            this.statusStrip1.TabIndex = 0x34;
            this.statusStrip1.Text = "statusStrip1";
            this.lblStatus.AutoToolTip = true;
            this.lblStatus.Name = "lblStatus";
            this.lblStatus.Size = new Size(0x150, 0x11);
            this.lblStatus.Spring = true;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.LightSteelBlue;
            base.ClientSize = new Size(0x15f, 230);
            base.Controls.Add(this.statusStrip1);
            base.Controls.Add(this.btnHelp);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnCheck);
            base.Controls.Add(this.lblRemain);
            base.Controls.Add(this.lblSpeed);
            base.Controls.Add(this.lblSize);
            base.Controls.Add(this.lblNewversion);
            base.Controls.Add(this.lblNewVersionLbl);
            base.Controls.Add(this.lblVersionLbl);
            base.Controls.Add(this.lblVersion);
            base.Controls.Add(this.lblTitle);
            base.Controls.Add(this.Bar);
            base.Controls.Add(this.lblMessage);
            this.DoubleBuffered = true;
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.Name = "xfmMyUpdate";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Cập Nhật Trực Tuyến";
            base.MinimumSizeChanged += new EventHandler(this.xfmMain_MinimumSizeChanged);
            base.Load += new EventHandler(this.xfmMain_Load);
            base.Shown += new EventHandler(this.xfmMain_Shown);
            base.Resize += new EventHandler(this.xfmMain_Resize);
            base.MaximumSizeChanged += new EventHandler(this.xfmMain_MaximumSizeChanged);
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void MyUtil_UnzipComplete(object sender)
        {
            if (this.Bar.InvokeRequired)
            {
                ZipHelper.UnzipCompleteEventHander method = new ZipHelper.UnzipCompleteEventHander(this.MyUtil_UnzipComplete);
                base.Invoke(method, new object[] { sender });
            }
            else
            {
                FileInfo info = new FileInfo(Application.StartupPath + @"\update.ini");
                if (info.Exists)
                {
                    info.Delete();
                }
                info = null;
                new FileInfo(Application.StartupPath + @"\Version.txt").CopyTo(Application.StartupPath + @"\update.ini", true);
                this.lblVersion.Text = this.lblNewversion.Text;
                this.btnCheck.Enabled = true;
                this.lblMessage.Text = "Cập Nhật Th\x00e0nh C\x00f4ng.";
                this.btnCheck.Text = "Kiểm Tra";
                this.btnCancel.Text = "Kết Th\x00fac";
            }
        }

        private void MyUtil_UnzipFail(object sender, string Message)
        {
            if (this.Bar.InvokeRequired)
            {
                ZipHelper.UnzipFailEventHander method = new ZipHelper.UnzipFailEventHander(this.MyUtil_UnzipFail);
                base.Invoke(method, new object[] { sender, Message });
            }
            else
            {
                this.lblMessage.Text = "Qu\x00e1 tr\x00ecnh cập nhật thất bại.";
                this.btnCancel.Text = "Kết Th\x00fac";
            }
        }

        private void MyUtil_UnzipProcess(object sender, long position)
        {
            if (this.Bar.InvokeRequired)
            {
                ZipHelper.UnzipProcessEventHander method = new ZipHelper.UnzipProcessEventHander(this.MyUtil_UnzipProcess);
                base.Invoke(method, new object[] { sender, position });
            }
            else
            {
                this.Bar.Value = (int) position;
            }
        }

        private void notify_Click(object sender, EventArgs e)
        {
            base.Show();
            base.WindowState = FormWindowState.Normal;
            this.notify.Visible = false;
        }

        private void xfmMain_Load(object sender, EventArgs e)
        {
        }

        private void xfmMain_MaximumSizeChanged(object sender, EventArgs e)
        {
            this.notify.Visible = false;
        }

        private void xfmMain_MinimumSizeChanged(object sender, EventArgs e)
        {
            this.notify.Visible = true;
        }

        private void xfmMain_Resize(object sender, EventArgs e)
        {
            if (base.WindowState == FormWindowState.Minimized)
            {
                base.Hide();
                this.notify.Visible = true;
            }
        }

        private void xfmMain_Shown(object sender, EventArgs e)
        {
            this.notify.Visible = false;
        }
    }
}

